package com.original.simuladoresws.controller;

import java.util.List;

import org.springframework.stereotype.Controller;

import com.original.simuladoresws.entities.TbComparsion;
import com.original.simuladoresws.entities.TbComparsionProduct;
import com.original.simuladoresws.entities.TbComparsionProductId;
import com.original.simuladoresws.entities.TbProduct;
import com.original.simuladoresws.entities.soap.SOAPBaseOutput;
import com.original.simuladoresws.entities.soap.SOAPFixedIncomeSaveInput;
import com.original.simuladoresws.service.ComparsionProductService;
import com.original.simuladoresws.service.ComparsionService;
import com.original.simuladoresws.service.impl.ComparsionProductServiceImpl;
import com.original.simuladoresws.service.impl.ComparsionServiceImpl;

@Controller
public class FixedIncomeController {
	ComparsionService comparsionService = new ComparsionServiceImpl();
	ComparsionProductService comparsionProductService = new ComparsionProductServiceImpl();
	public SOAPBaseOutput fixedIncomeSave(SOAPFixedIncomeSaveInput input) {
		SOAPBaseOutput output = new SOAPBaseOutput();
		TbComparsionProduct comProduct;
		TbComparsionProductId idComPrt;
		TbComparsion comparsion = new TbComparsion();
		// set data comparsion
		comparsion.setId(comparsionService.getId());//TODO
		comparsion.setNmUserName(input.getName());
		comparsion.setDsUserEmail(input.getEmail());
		comparsion.setDsUserDocument(input.getDocument());
		comparsion.setDsUserEmail(input.getEmail());
		comparsion.setDsUserTelephone2(input.getCelular());
		comparsion.setVlInitialAmount(input.getAmount());
		comparsion.setVlInvestmentTime(input.getInvestment_Time());
		comparsion.setVlInvestmentTimeType(input.getInvestment_Time_Type());
		
		
		List<TbProduct> products = input.getProducts();
		if (products != null) {
			// save comparsiom
			Integer id = null;
			try {
				id = comparsionService.save(comparsion);
				output.setError(false);
				output.setMessage("");
			} catch (Exception e) {
				output.setError(true);
				output.setMessage("");
			}
			// save comparsionProduct
			for (TbProduct prt : products) {
				comProduct = new TbComparsionProduct();
				idComPrt = new TbComparsionProductId();
				idComPrt.setIdProduct(prt.getId());
				idComPrt.setIdComparsion(id);
				comProduct.setId(idComPrt);
				try {
					comparsionProductService.save(comProduct);
					output.setError(false);
					output.setMessage("");
				} catch (Exception e) {
					output.setError(true);
					output.setMessage("");
				}
			}
		}
		return output;
	}
}
